package zhihui.mobile.presenter.register;

import android.content.Context;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;
import zhihui.mobile.bean.register.RegisterInfo;
import zhihui.mobile.bean.user.UserInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.register.RegisterManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.receiver.ExampleUtil;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.register.RegisterActivity;
import zhihui.mobile.uitils.CheckUtils;

/**
 * Created by 张公铭 on 2017/7/12 0012.
 */

public class PasswordFragmentPresenter implements IPasswordContract.IPasswordPresenter
{
    private Context mContext;
    private IPasswordContract.IPasswordView passwordView;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        passwordView = (IPasswordContract.IPasswordView) view;
    }

    @Override
    public void onResume()
    {
        passwordView.showMessage("");
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void register(final String password)
    {
        String msg = CheckUtils.checkPassword(password);
        if (!TextUtils.isEmpty(msg))
        {
            passwordView.showMessage(msg);
            return;
        }
        passwordView.showLoading();
        RegisterManager.getInstance().getCurRegister().setPassword(password);
        RegisterManager.getInstance().getCurRegister().setClietid(ExampleUtil.getImei(mContext, ""));
        RegisterManager.getInstance().getCurRegister().setToken(ExampleUtil.getDeviceId(mContext));
        RegisterManager.getInstance().register(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                passwordView.hindLoading();
                try
                {
                    String state = result.getString("state");
                    String message = result.getString("msg");
                    if (state.equals(Config.SUCCESS))
                    {
                        passwordView.showMessage("注册成功！");
                        UserInfo userInfo = new UserInfo();
                        RegisterInfo registerInfo = RegisterManager.getInstance().getCurRegister();
                        userInfo.setTel(registerInfo.getPhone());
                        userInfo.setName("");
                        userInfo.setNo(registerInfo.getPhone());
                        userInfo.setPwd(registerInfo.getPassword());
                        userInfo.setSex(0);
                        userInfo.setClientid(JPushInterface.getRegistrationID(mContext));

                        UserManager.getInstance().setCurUser(userInfo);
                        passwordView.goBack(RegisterActivity.class);
                    } else
                    {
                        passwordView.showMessage(message);
                    }

                } catch (JSONException e)
                {
                    passwordView.hindLoading();
                    e.printStackTrace();
                    passwordView.showMessage("解析数据错误！");
                }
            }

            @Override
            public void failed(String message)
            {
                passwordView.hindLoading();
                passwordView.showMessage(message);
            }
        });
    }
}
